package com.xxx.car.web.api;

import com.xxx.car.entity.TbAppeal;
import com.xxx.car.entity.TbInvoice;
import com.xxx.car.entity.TbTravelOrder;
import com.xxx.car.entity.vo.request.*;
import com.xxx.car.entity.vo.response.RespInvoiceDetail;
import com.xxx.car.entity.vo.response.RespTravelOrderDetail;
import com.xxx.car.entity.vo.response.RespUserTravelOrder;
import com.xxx.car.service.*;
import com.xxx.common.base.enums.ApiResponseEnum;
import com.xxx.common.base.vo.APIResponse;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by yxj on 17/6/15.
 */
@RestController
@Api(value = "/api/user", description = "用户API接口")
@RequestMapping("/api/user")
public class ApiUserController {
	@Value("${upload.filePath}")
	private String filePath;
	@Autowired
	UserService userService;
	@Autowired
	TravelAppealService travelAppealService;
	@Autowired
	CommonService commonService;
	@Autowired
	RedpacketService redpacketService;
	@Autowired
	InvoiceService invoiceService;

	/**
	 * 用户生成邀请码
	 * @param reqUser
	 * @return
	 */
	@RequestMapping(value = "/shareCode")
	public APIResponse shareCode(@Valid @ModelAttribute ReqUser reqUser, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
		}
		APIResponse response = userService.getShareCode(reqUser.getUserId());
		return response;
	}


	/**
	 * 用户详情
	 * @param reqUser
	 * @return
	 */
	@RequestMapping(value = "/userDetail")
	public APIResponse userDetail(@Valid @ModelAttribute ReqUser reqUser, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
		}
		APIResponse response = userService.userDetail(reqUser.getUserId());
		return response;
	}

	/**
	 * 用户修改密码
	 * @param reqUserLogin
	 * @return
	 */
	@RequestMapping(value = "passwordUpdate")
	public APIResponse<?> passwordUpdate(@Valid @ModelAttribute ReqUserLogin reqUserLogin, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
		}
		APIResponse response = null;
		try {
			response = userService.modifyUserPassword(reqUserLogin.getMobile(),reqUserLogin.getPassword());
		} catch (Exception e) {
			e.printStackTrace();
			return APIResponse.returnFail(e.getMessage());
		}
		return response;
	}

	/**
	 * APP用户上传图片
	 * @param file
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/uploadUserImage")
	public APIResponse uploadUserImage(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
		String fileName = null;
		try {
			fileName = commonService.uploadFile(file);
		} catch (Exception e) {
			e.printStackTrace();
			return APIResponse.returnFail(e.getMessage());
		}
		return APIResponse.returnSuccess(fileName);
	}

	/**
	 * 退出登陆
	 * @return
	 */
	@RequestMapping(value = "logout")
	public APIResponse<?> logout(HttpServletRequest request){
		String userCookieName = (String)request.getAttribute("userCookieName");
		APIResponse response = userService.logout(userCookieName);
		return response;
	}

	/**
	 * 用户上传头像
	 * @param file
	 * @param request
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/uploadIcon")
	public APIResponse uploadUserIcon(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
		Integer userId = (Integer) request.getAttribute("userId");
		if (!file.isEmpty()) {
			return userService.uploadUserIcon(file,userId);
		} else {
			return APIResponse.returnFail("请选择图片");
		}
	}

	/**
	 * 修改用户昵称
	 * @param nickName
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "modifyUserNickName")
	public APIResponse<?> modifyUserNickName(@RequestParam String nickName, HttpServletRequest request){
		Integer userId = (Integer) request.getAttribute("userId");
		if (StringUtils.isEmpty(nickName)){
			return APIResponse.returnFail("请输入昵称");
		}
		APIResponse response = userService.modifyUserNickName(nickName,userId);
		return response;
	}

	/**
	 * 用户行程列表
	 * @param reqUserTravelOrderQuery
	 * @return
	 */
	@RequestMapping(value = "travel")
	public APIResponse<?> travel(@Valid @ModelAttribute ReqUserTravelOrderQuery reqUserTravelOrderQuery, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
		}
		RespUserTravelOrder respUserTravelOrder = travelAppealService.queryUserTravelOrderList(reqUserTravelOrderQuery);
		return APIResponse.returnSuccess(respUserTravelOrder);
	}

	/**
	 * 用户行程详情
	 * @param reqUserTravelOrderQuery
	 * @return
	 */
	@RequestMapping(value = "travelDetail")
	public APIResponse<?> travelDetail(@Valid @ModelAttribute ReqUserTravelOrderQuery reqUserTravelOrderQuery, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
		}
		if (reqUserTravelOrderQuery.getId() == null){
			return APIResponse.returnFail("行程单id必传");
		}

		TbTravelOrder tbTravelOrder = travelAppealService.queryUserTravelOrderDetail(reqUserTravelOrderQuery);
		TbAppeal tbAppeal = travelAppealService.queryUserAppealDetailByTravelOrderId(reqUserTravelOrderQuery.getId());
		boolean canSnatchRedpacket = redpacketService.canSnatchRedpacket(reqUserTravelOrderQuery.getId());

		RespTravelOrderDetail respTravelOrderDetail = new RespTravelOrderDetail();
		respTravelOrderDetail.setTbTravelOrder(tbTravelOrder);
		respTravelOrderDetail.setTbAppeal(tbAppeal);
		respTravelOrderDetail.setCanSnatchRedpacket(canSnatchRedpacket);

		return APIResponse.returnSuccess(respTravelOrderDetail);
	}

	/**
	 * 用户申诉列表
	 * @param reqUserAppealQuery
	 * @return
	 */
	@RequestMapping(value = "appeal")
	public APIResponse<?> appeal(@Valid @ModelAttribute ReqUserAppealQuery reqUserAppealQuery, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
		}
		List<TbAppeal> tbAppealList = travelAppealService.queryUserAppealList(reqUserAppealQuery);
		return APIResponse.returnSuccess(tbAppealList);
	}

	/**
	 * 用户申诉详情
	 * @param reqUserAppealQuery
	 * @return
	 */
	@RequestMapping(value = "appealDetail")
	public APIResponse<?> appealDetail(@Valid @ModelAttribute ReqUserAppealQuery reqUserAppealQuery, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
		}
		TbAppeal tbAppeal = travelAppealService.queryUserAppealDetail(reqUserAppealQuery);
		return APIResponse.returnSuccess(tbAppeal);
	}

	/**
	 * 用户申诉修改
	 * @param reqAppeal
	 * @return
	 */
	@RequestMapping(value = "appealUpdate")
	public APIResponse<?> appealUpdate(@Valid @ModelAttribute ReqAppeal reqAppeal, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
		}
		try {
			travelAppealService.updateUserAppealDetail(reqAppeal);
		} catch (Exception e) {
			e.printStackTrace();
			return APIResponse.returnFail(e.getMessage());
		}
		return APIResponse.returnSuccess();
	}

	/**
	 * 用户申诉提交
	 * @param reqAppeal
	 * @return
	 */
	@RequestMapping(value = "appealAdd")
	public APIResponse<?> appealAdd(@Valid @ModelAttribute ReqAppeal reqAppeal, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
		}
		APIResponse response = travelAppealService.submitUserAppeal(reqAppeal);
		return response;
	}

	/**
	 * 用户抢红包
	 * @param reqUserRedpacketQuery
	 * @return
	 */
	@RequestMapping(value = "snatchRedpacket")
	public APIResponse<?> snatchRedpacket(@Valid @ModelAttribute ReqUserRedpacketQuery reqUserRedpacketQuery, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
		}
		APIResponse response = redpacketService.snatchRedpacket(reqUserRedpacketQuery.getTravelOrderId(),reqUserRedpacketQuery.getUserId());
		return response;
	}

	/**
	 * 红包详情
	 * @param reqUserRedpacketQuery
	 * @return
	@RequestMapping(value = "redpacketDetail")
	public APIResponse<?> redpacketDetail(@Valid @ModelAttribute ReqUserRedpacketQuery reqUserRedpacketQuery, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
		}
		APIResponse response = null;
		return response;
	}*/

	/**
	 * 用户发票列表
	 * @param reqInvoiceQuery
	 * @return
	 */
	@RequestMapping(value = "invoice")
	public APIResponse<?> invoice(@Valid @ModelAttribute ReqInvoiceQuery reqInvoiceQuery, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
		}
		List<TbInvoice> tbInvoiceList = invoiceService.queryInvoiceList(reqInvoiceQuery.getUserId());
		return APIResponse.returnSuccess(tbInvoiceList);
	}

	/**
	 * 用户发票申请
	 * @param reqInvoice
	 * @return
	 */
	@RequestMapping(value = "invoiceApply")
	public APIResponse<?> invoiceApply(@Valid @ModelAttribute ReqInvoice reqInvoice, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
		}
		APIResponse response = invoiceService.applyInvoice(reqInvoice);
		return response;
	}

	/**
	 * 用户发票详情
	 * @param reqInvoiceQuery
	 * @return
	 */
	@RequestMapping(value = "invoiceDetail")
	public APIResponse<?> invoiceDetail(@Valid @ModelAttribute ReqInvoiceQuery reqInvoiceQuery, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
		}
		if (reqInvoiceQuery.getId() == null) {
			return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
		}
		RespInvoiceDetail respInvoiceDetail = invoiceService.queryInvoiceDetail(reqInvoiceQuery);
		return APIResponse.returnSuccess(respInvoiceDetail);
	}

	/**
	 * 用户发票更新
	 * @param reqInvoice
	 * @return
	 */
	@RequestMapping(value = "invoiceUpdate")
	public APIResponse<?> invoiceUpdate(@Valid @ModelAttribute ReqInvoice reqInvoice, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
		}
		APIResponse response = invoiceService.updateInvoice(reqInvoice);
		return response;
	}
}
